package service.edu.controller;


import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import service.edu.entity.Chapter;
import service.edu.entity.chapter.ChapterVo;
import service.edu.service.ChapterService;
import service.edu.result.R;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author diao
 * @since 2020-08-05
 */
@RestController
@CrossOrigin
@RequestMapping("/edu/chapter")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    @ApiOperation("以树形形式显示课程的章节与小节")
    @GetMapping("/queryAllChapters/{courseId}")
    public R queryChapter(@ApiParam(name = "courseId",value = "课程id")@PathVariable("courseId")String courseId){
        //一级分类的属性包含二级分类
        List<ChapterVo> chapterList= chapterService.getAllChapters(courseId);
        return R.Ok().data("chapterList",chapterList);
    }

    @ApiOperation("添加章节")
    @PostMapping("addChapter")
    public R addChapter(@ApiParam(name = "chapter",value = "新增的章节")@RequestBody(required = false)Chapter chapter){
        chapterService.save(chapter);
        return R.Ok();
    }

    @ApiOperation("修改章节")
    @PostMapping("updateChapter")
    public R updateChapter(@ApiParam(name = "chapter",value = "修改的章节")@RequestBody(required = false)Chapter chapter)
    {
        chapterService.updateById(chapter);
        return R.Ok();
    }

    @ApiOperation("当没有小节内容时删除章节")
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapter(@ApiParam(name = "chapterId",value = "被删除章节对应的id")@PathVariable("chapterId")String chapterId){
        boolean flag = chapterService.deleteChapterById(chapterId);
        return R.Ok().data("flag", flag);
    }

    @ApiOperation("根据id查询章节")
    @GetMapping("queryChapter/{chapterId}")
    public R queryChapterById(@ApiParam(name = "chapterId",value = "章节对应的id")@PathVariable("chapterId")String chapterId){
        Chapter chapter = chapterService.getById(chapterId);
        return R.Ok().data("chapter",chapter);
    }
}

